﻿Imports System.IO
Imports Control_Vuelos_BE
Imports Control_Vuelos_BL

Public Class frmAdm_ManifiestoAdjunto
    Public p_Codviaje As String
    Private ReadOnly _CMAdjunto As New C_Manifiesto_AdjuntoBL()

    Private Sub frmAdm_ManifiestoAdjunto_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        AddHandler ModAdm_Adjuntos1.BtnExaminar.Click, AddressOf prl_AdjuntarArchivo
        AddHandler ModAdm_Adjuntos1.GridControl1.DoubleClick, AddressOf prl_ViewAdjunto
        ModAdm_Adjuntos1.GridControl1.DataSource = _CMAdjunto.pr_ListarManifiestoAdjunto(p_Codviaje)
        ModAdm_Adjuntos1.GridView1.BestFitColumns()
        AddHandler ModAdm_Adjuntos1.cmdAceptar.Click, AddressOf prl_CloseAdjunto
        AddHandler ModAdm_Adjuntos1.label1.DragDrop, AddressOf prl_EliminarAdjunto
        AddHandler ModAdm_Adjuntos1.BarManager1.ItemClick, AddressOf prl_EliminarAdjuntoGrid
    End Sub

    Public Sub prl_EliminarAdjuntoGrid(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        If e.Item.Caption = "Eliminar adjunto" Then
            Try
                If ModAdm_Adjuntos1.GridView1.RowCount > 0 Then
                    Dim Rows As New ArrayList()
                    Rows.Add(ModAdm_Adjuntos1.GridView1.GetDataRow(ModAdm_Adjuntos1.GridView1.GetSelectedRows()(0)))
                    Dim Row As DataRow = CType(Rows(0), DataRow)
                    If Not Row Is Nothing Then
                        If _CMAdjunto.pr_EliminarAdjunto(Row("CodViaje"), Row("CodAdjunto")) Then
                            TryCast(ModAdm_Adjuntos1.GridControl1.DataSource, DataTable).Rows.RemoveAt(ModAdm_Adjuntos1.GridView1.GetSelectedRows()(0))
                        End If
                    End If
                End If
            Catch myException As Exception
                MsgBox("(BarManager1.ItemClick) --> " & myException.Message, MsgBoxStyle.Critical)
            End Try
            ModAdm_Adjuntos1.label1.ImageIndex = 0
        End If
    End Sub

    Public Sub prl_EliminarAdjunto(sender As Object, e As System.Windows.Forms.DragEventArgs)
        Try
            If ModAdm_Adjuntos1.GridView1.RowCount > 0 Then
                Dim Rows As New ArrayList()
                Rows.Add(ModAdm_Adjuntos1.GridView1.GetDataRow(ModAdm_Adjuntos1.GridView1.GetSelectedRows()(0)))
                Dim Row As DataRow = CType(Rows(0), DataRow)
                If Not Row Is Nothing Then
                    If _CMAdjunto.pr_EliminarAdjunto(Row("CodViaje"), Row("CodAdjunto")) Then
                        TryCast(ModAdm_Adjuntos1.GridControl1.DataSource, DataTable).Rows.RemoveAt(ModAdm_Adjuntos1.GridView1.GetSelectedRows()(0))
                    End If
                End If
            End If
        Catch myException As Exception
            MsgBox("(label1_DragDrop) --> " & myException.Message, MsgBoxStyle.Critical)
        End Try
        ModAdm_Adjuntos1.label1.ImageIndex = 0
    End Sub

    Public Sub prl_CloseAdjunto()
        Close()
    End Sub

    Public Sub prl_ViewAdjunto()
        Try
            Dim Rows As New ArrayList()
            Rows.Add(ModAdm_Adjuntos1.GridView1.GetDataRow(ModAdm_Adjuntos1.GridView1.GetSelectedRows()(0)))
            Dim Row As DataRow = CType(Rows(0), DataRow)
            If Not Row Is Nothing Then
                Dim RCAdjunto As New C_Manifiesto_Adjunto
                Dim bytes1() As Byte
                RCAdjunto = _CMAdjunto.pr_MostrarAdjunto(p_Codviaje, Row("CodAdjunto"))
                bytes1 = RCAdjunto.FileAdjunto
                Dim sfile As String
                sfile = RCAdjunto.NomAdjunto
                Dim ExtensionNombre As String = String.Format("{0}\{1}", Application.StartupPath(), sfile)
                Dim fs As System.IO.FileStream = New System.IO.FileStream(ExtensionNombre, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)
                Dim bw As System.IO.BinaryWriter
                bw = New System.IO.BinaryWriter(fs)
                bw.Write(bytes1)
                bw.Flush()
                bw.Close()
                fs.Close()
                Using Command As New Process()
                    'Creamos la instancia Process
                    Command.StartInfo.FileName = ExtensionNombre
                    Command.StartInfo.UseShellExecute = True
                    Command.StartInfo.CreateNoWindow = True
                    Command.Start()
                End Using
            End If
        Catch myException As Exception
            MsgBox(" Error " & myException.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Public Sub prl_AdjuntarArchivo()
        Using dialog As New OpenFileDialog() With {.Title = "Abrir archivo para adjuntar al Manifiesto", .Filter = "All Files|*.msg;*.jpg;*.bmp;*.jpeg;*.xls;*.xlsx;*.doc;*.docx;*.pdf;*.txt;*.zip;*.pptx;*.ppt|Excel Files|*.xls;*.xlsx|Documents Files|*.doc;*.docx|Pdfs Files|*.pdf|Image Files|*.jpg;*.bmp;*.jpeg", .FilterIndex = 1, .RestoreDirectory = True}
            If dialog.ShowDialog() = DialogResult.OK Then
                Dim imageToInsert As String = dialog.FileName
                ModAdm_Adjuntos1.gFileAdjunto.EditValue = dialog.FileName
                Dim Archivo As New FileInfo(dialog.FileName)
                Dim bytes() As Byte
                Dim fs As FileStream
                fs = New FileStream(dialog.FileName, FileMode.Open, FileAccess.Read)
                ReDim bytes(fs.Length)
                fs.Read(bytes, 0, fs.Length)
                fs.Close()
                Dim secuencias As Integer = _CMAdjunto.pr_NumeroSiguiente(p_Codviaje)
                Dim _RCAdjunto As New C_Manifiesto_Adjunto()
                Dim valor As String = "00" & Trim(CType(_CMAdjunto.pr_NumeroSiguiente(p_Codviaje), String))
                _RCAdjunto.CodViaje = p_Codviaje
                _RCAdjunto.CodAdjunto = valor.Substring(valor.Length - 2, 2)
                _RCAdjunto.NomAdjunto = dialog.SafeFileName
                _RCAdjunto.FileAdjunto = bytes
                _RCAdjunto.FchAdjunto = Archivo.CreationTime
                _RCAdjunto.SizAdjunto = Archivo.Length
                If _CMAdjunto.pr_AgregarAdjuntoViaje(_RCAdjunto) Then
                    ModAdm_Adjuntos1.GridControl1.DataSource = _CMAdjunto.pr_ListarManifiestoAdjunto(p_Codviaje)
                End If
            End If
        End Using
    End Sub
End Class